package com.cskaoyan.config;


import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class String2DateConverter implements Converter<String, Date> {

    @SneakyThrows
    @Override
    public Date convert(String s) {
        if (s.length() == 10){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                return dateFormat.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (s.length() == 19) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.parse(s);
        }
        return null;
    }
}
